package model.entity;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import model.GameClock;
import model.entity.stats.Stat;
import model.entity.stats.StatSet;
import model.item.EquippedInventory;
import model.item.Inventory;
import model.map.Direction;
import model.map.Location;
import model.map.MovementType;
import utilities.*;

/**
 * 
 * This is the base Entity that contains various
 * operations that can be done on both Avatar,
 * NPCs, etc
 *
 */

public abstract class Entity extends BasicEntity implements Archiveable {
	protected Occupation occupation;
	protected Boolean sleeping;

	public Entity() {
		
	}
	
	public Entity(String name, Direction direction, Location location, StatSet stats, 
			List<MovementType> movementTypes, Inventory inventory, 
			EquippedInventory equipment, Occupation occupation, Boolean sleeping){

		super(name, direction, location, stats, 
				movementTypes, inventory, equipment);

		this.occupation = occupation;
		this.sleeping = sleeping;
	}

	public Occupation getOccupation() {
		return occupation;
	}

	public void setOccupation(Occupation occupation) {
		this.occupation = occupation;
	}

	public Boolean getSleeping() {
		return sleeping;
	}

	public void setSleeping(Boolean sleeping) {
		this.sleeping = sleeping;
	}

	public void makeSleepy(Long tickCount){

		sleeping = true;

		/*GameClock.getInstance.schedule(tickCount, new Executable() {
			public void execute(){
				sleeping = false;
			}
		});*/
	}

	public void levelUp(){
		StatSet luss = occupation.getLevelUpStats();
		Map<String, Stat> listLUSS = luss.getStats();

		Iterator it = listLUSS.entrySet().iterator();
		while (it.hasNext()){
			Map.Entry pairs = (Map.Entry)it.next();
			//Stat currentStat = stats.removeStat((String)pairs.getKey());
			//currentStat.addToBaseValue((Double)pairs.getValue());
			stats.addConstantModifier((String)pairs.getKey(), (Double)pairs.getValue());
		}
	}

	public void die(){
		location = null;
	}

	
	public Archive save() {
		Archive archive = super.save();
		
//		archive.putClassName("occupation", this.occupation.getClass().getName());
//		archive.putOneRelationship("occupation", this.occupation);
//		
//		archive.putAttribute("sleeping", Boolean.toString(this.sleeping));
		
		return archive;
	}
	
	public void load(Archive archive) {
		super.load(archive);
		
//		this.occupation = (Occupation)ModelFactory.getInstance().createInstanceWithClassName(archive.getClassName("occupation"));
//		this.occupation.load(archive.getOneRelationship("occupation"));
//		
//		this.sleeping = Boolean.parseBoolean(archive.getAttribute("sleeping"));
	}
	
}
